package com.esen.dbpool;

import java.sql.Connection;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DataSourceFactory {
	private static volatile boolean inited = false;

	private static DruidDataSource dataSource = null;

	public static DruidDataSource initDataSource() throws Exception {
		Properties prop = new Properties();
		prop.load(DataSourceFactory.class.getClassLoader().getResourceAsStream("druid.properties"));
		dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(prop);
		return dataSource;
	}

	public static synchronized Connection getConnection() throws Exception {
		if (!inited) {
			initDataSource();
			inited = true;
		}
		return dataSource.getConnection();
	}
}
